package com.lzy.mall.ware.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lzy.mall.common.constant.WareConstant;
import com.lzy.mall.common.utils.PageParaUtils;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.ware.entity.WmsPurchase;
import com.lzy.mall.ware.dao.WmsPurchaseDao;
import com.lzy.mall.ware.entity.WmsPurchaseDetail;
import com.lzy.mall.ware.service.WmsPurchaseDetailService;
import com.lzy.mall.ware.service.WmsPurchaseService;
import com.lzy.mall.ware.vo.MergeVo;
import com.lzy.mall.ware.vo.PurchaseDoneVo;
import org.beetl.sql.core.engine.PageQuery;
import org.beetl.sql.core.query.LambdaQuery;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 采购信息(WmsPurchase)表服务实现类
 *
 * @author lzy
 * @since 2021-08-24 19:01:40
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class WmsPurchaseServiceImpl implements WmsPurchaseService {
    private final WmsPurchaseDao purchaseDao;

    private final WmsPurchaseDetailService purchaseDetailService;

    @Override
    public void removeBatch(ArrayList<Long> ids) {
        purchaseDao.createLambdaQuery()
                .andIn(WmsPurchase::getId,ids)
                .delete();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);

        LambdaQuery<WmsPurchase> lambdaQuery = purchaseDao.createLambdaQuery();

        String key = (String) params.get("key");
        if (StrUtil.isNotEmpty(key)){
            lambdaQuery.and(lambdaQuery.condition()
                    .andEq(WmsPurchase::getAssigneeId,key)
            );
        }
        String status = (String) params.get("status");
        if (StrUtil.isNotEmpty(status)){
            lambdaQuery.andEq(WmsPurchase::getStatus,status);
        }
        PageQuery<WmsPurchase> page = lambdaQuery.page(pageQuery.getPageNumber(), pageQuery.getPageSize());
        return new PageUtils(page);
    }

    @Override
    public WmsPurchase getById(Long id) {
        return purchaseDao.single(id);
    }

    @Override
    public void save(WmsPurchase purchase) {
        Date now = DateUtil.date();
        purchase.setCreateTime(now);
        purchase.setUpdateTime(now);
        purchaseDao.insertTemplate(purchase);
    }

    @Override
    public void update(WmsPurchase purchase) {
        purchase.setUpdateTime(DateUtil.date());
        purchaseDao.updateTemplateById(purchase);
    }

    @Override
    public PageUtils queryPageUnreceive(Map<String, Object> params) {
        PageQuery pageQuery = PageParaUtils.pagePara(params);

        PageQuery<WmsPurchase> page = purchaseDao
                .createLambdaQuery()
                .andEq(WmsPurchase::getStatus, WareConstant.PurchaseStatusEnum.CREATED.getCode())
                .orEq(WmsPurchase::getStatus,WareConstant.PurchaseStatusEnum.ASSIGNED.getCode())
                .page(pageQuery.getPageNumber(), pageQuery.getPageSize());
        return new PageUtils(page);
    }

    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Date now = DateUtil.date();
        Long purchaseId = mergeVo.getPurchaseId();
        if(ObjectUtil.isNull(purchaseId)){
            //1、新建一个
            WmsPurchase purchase = new WmsPurchase();

            purchase.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchase.setCreateTime(now);
            purchase.setUpdateTime(now);
            purchaseDao.insertTemplate(purchase,true);
            purchaseId = purchase.getId();
        }

        //TODO 确认采购单状态是0,1才可以合并

        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;
        List<WmsPurchaseDetail> collect = items.stream().map(i -> {
            WmsPurchaseDetail detailEntity = new WmsPurchaseDetail();
            detailEntity.setId(i);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
            return detailEntity;
        }).collect(Collectors.toList());


        purchaseDetailService.updateBatchById(collect);

        WmsPurchase purchase = new WmsPurchase();
        purchase.setId(purchaseId);
        purchase.setUpdateTime(new Date());
        update(purchase);
    }

    @Override
    @Transactional
    public void received(Long[] ids) {
        //TODO 登陆人id
        //1、确认当前采购单是新建或者已分配状态
        ArrayList<Long> longs = CollUtil.toList(ids);
        List<WmsPurchase> list = purchaseDao.createLambdaQuery()
                .andIn(WmsPurchase::getId, longs)
                .select()
                .stream()
                .filter(item ->
                        (WareConstant.PurchaseStatusEnum.CREATED.getCode().equals(item.getStatus())
                                || WareConstant.PurchaseStatusEnum.ASSIGNED.getCode().equals(item.getStatus())))
                .map(item -> {
                    item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
                    item.setUpdateTime(DateUtil.date());
                    return item;
                })
                .collect(Collectors.toList());

        //2、改变采购单的状态
        purchaseDao.getSQLManager()
                .updateBatchTemplateById(WmsPurchase.class,list);

        //3、改变采购项的状态
        List<Long> purchaseIds = list.stream()
                .map(WmsPurchase::getId)
                .collect(Collectors.toList());
        purchaseDetailService.updateDetailByPurchaseIds(purchaseIds);
    }

    @Override
    public void done(PurchaseDoneVo purchaseDoneVo) {

    }
}
